<template>
  <rx-dialog @handOk="handleSubmit" :oktext="i18n('groupRel.joinUserBtn','加入用户')" btnalign="left" order="top" @cancel="cancel">
    <div>
      <a-table
        rowKey="userId"
        :pagination="false"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :columns="columns"
        :dataSource="relData">
          <span slot="action" slot-scope="text, record">
            <a @click="removeRel(record)">{{$ti18('rxList.removeBtn','移除')}}</a>
          </span>
      </a-table>
    </div>
  </rx-dialog>
</template>

<script>
  import {Dialog, RxDialog, Util} from "@lowcode/jpaas-common-lib"
    import OsRelInstApi from '@/api/user/org/osRelInst'

    export default {
      name: "OsRelInstGroupRelUsers",
      components: {
        RxDialog
      },
      props: ["layerid","destroy","p1","p2","dimId1","dimId2","relTypeId","tenantId"],
      data() {
        return {
          selectedRowKeys: [],
          selectedRows: [],
          relData: [],
          columns: [{
            title: this.$ti18('rxList.action','操作'),
            indentWidth: '180px',
            dataIndex: 'action',
            scopedSlots: {customRender: 'action'}
          }, {
            title: this.i18n('form.name','姓名'),
            dataIndex: 'fullName'
          }, {
              title: this.i18n('groupRel.userNo','编号'),
              dataIndex: 'userNo'
          }]
        }
      },
      computed: {
      },
      watch: {
      },
      created() {
        this.init();
      },
      methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"osGroup",key);
        },
        init(){
            var params={p1:this.p1,p2:this.p2,relTypeId:this.relTypeId};
            if(this.tenantId){
                params.tenantId=this.tenantId;
            }

            OsRelInstApi.groupRelUsersData(params).then(res=>{
              this.relData=res.data;
            })
        },
        handleSubmit(e) {
          var params={single:false};
          if(this.tenantId){
              params.tenantId=this.tenantId;
          }

          var self_=this;
          e.loading=false;
          Dialog.openUserDialog({
              curVm: this,data:params, widthHeight: ['1100px', '600px']
          },function(self,users) {
            var userIds=[];

            for(var i=0;i<users.length;i++){
              userIds.push(users[i].userId);
            }
            var parameter={
                userIds:userIds.join(','),
                relTypeId:self.relTypeId,
                p1:self.p1,
                p2:self.p2
            }
            if(self_.tenantId){
                parameter.tenantId=self_.tenantId;
            }

            OsRelInstApi.joinTwoGroupUsers(parameter).then(res=>{
              self.init();
              e.loading=false;
            })
          })
        },
        cancel() {
          Util.closeWindow(this,"cancel")
        },
        removeRel(record){
          OsRelInstApi.delByTwoPartyUserId({
            userId:record.userId,
            relTypeId:this.relTypeId,
            p1:this.p1,
            p2:this.p2
          }).then(res=>{
            this.init();
          })
        },
        onSelectChange (selectedRowKeys, selectedRows) {
          this.selectedRowKeys = selectedRowKeys
          this.selectedRows = selectedRows
        }
      }
    }
</script>

<style scoped>

</style>